/*
 * Copyright (c) 2009 Tamás Kovács <keccs *at* inbox.com>
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * 
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 * 
 */
package keccs.hitTheHay.ui;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class Main implements Runnable {
	
	public static void main(String[] args) throws Throwable {
		String tempFile = extractDllToTempFile();
		System.load(tempFile);
		
		trySettingLookAndFeel();
		SwingUtilities.invokeLater(new Main());
	}

	private static String extractDllToTempFile() throws IOException, FileNotFoundException {
		String dllName = "ni" + System.getProperty("sun.arch.data.model") + ".dll";
		
		File tempFile = File.createTempFile("hit-the-hay-", ".dll");
		tempFile.delete();
		
		tempFile = new File(tempFile.getParent(), "hit-the-hay.dll");
		tempFile.delete();
		tempFile.createNewFile();
		
		
		InputStream dllStream = null;
		OutputStream tempFileStream = null;

		try {
			// copy from jar
			dllStream = Main.class.getClassLoader().getResourceAsStream("lib/" + dllName);
			
			// else copy from lib dir in project
			if (dllStream == null)
				dllStream = new FileInputStream("lib/" + dllName);
						
			tempFileStream = new FileOutputStream(tempFile);
			
			int bytesRead;
			byte[] buffer = new byte[4096];
			while ((bytesRead = dllStream.read(buffer)) > 0)
				tempFileStream.write(buffer, 0, bytesRead);
		} finally {
			close(tempFileStream);
			close(dllStream);
		}
		
		return tempFile.getAbsolutePath();
	}

	private static void trySettingLookAndFeel() {
		try {
			UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
		} catch (Exception e1) {
			System.err.println("Cannot set nimbus LNF (see stack trace), trying system LNF...");
			e1.printStackTrace();

			try {
				UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
			} catch (Exception e2) {
				System.err.println("Cannot set system LNF...");
				e2.printStackTrace();
			}
		}
	}

	@Override
	public void run() {
		new MainFrame();
	}
	
	private static void close(OutputStream stream) {
		if (stream != null) { 
			try {
				stream.close();
			} catch (IOException e) {
				e.printStackTrace(System.err);
			}
		}
	}
	
	private static void close(InputStream stream) {
		if (stream != null) { 
			try {
				stream.close();
			} catch (IOException e) {
				e.printStackTrace(System.err);
			}
		}
	}
}
